package com.ruoyi.web.controller.category.admin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.model.category.Category;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.category.service.ICategoryService;

import java.util.List;

/**
 * @author : 张怀秋
 **/
@Api(tags = "项目分类管理")
@Slf4j
@RestController
@RequestMapping("/admin/category")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CategoryController extends BaseController {
    private final ICategoryService categoryService;

    @ApiOperation("获取树形结构的分类列表数据")
    @GetMapping("/findTreeCategoryList")
    public AjaxResult findTreeCategoryList(@ApiParam("分类名称") String categoryName) {
        return AjaxResult.success(this.categoryService.queryTreeCategoryList(categoryName));
    }

    @ApiOperation("将树形结构的分类列表展开-----竞赛专属")
    @GetMapping("/findExpandContestCategoryList")
    public AjaxResult findExpandContestCategoryList(@ApiParam("分类名称") String categoryName) {
        return AjaxResult.success(this.categoryService.queryExpandContestCategoryList(categoryName));
    }

    @ApiOperation("获取树形结构的分类列表数据-----竞赛专属")
    @GetMapping("/findTreeContestCategoryList")
    public AjaxResult findTreeContestCategoryList(@ApiParam("分类名称") String categoryName) {
        return AjaxResult.success(this.categoryService
                .queryTreeContestCategoryList(categoryName));
    }

    @ApiOperation("获取树形结构的分类列表数据-----活动专属")
    @GetMapping("/findTreeActivityCategoryList")
    public AjaxResult findTreeActivityCategoryList(@ApiParam("分类名称") String categoryName) {
        return AjaxResult.success(this.categoryService
                .queryTreeActivityCategoryList(categoryName));
    }

    @ApiOperation("获取树形结构的分类列表数据-----文章专属")
    @GetMapping("/findTreeBlogCategoryList")
    public AjaxResult findTreeBlogCategoryList(@ApiParam("分类名称") String categoryName) {
        return AjaxResult.success(this.categoryService
                .queryTreeBlogCategoryList(categoryName));
    }

    @ApiOperation("查询所有一级分类")
    @GetMapping("/findCategory1List")
    public AjaxResult findCategory1List() {
        return AjaxResult.success(this.categoryService.list(Wrappers.<Category>lambdaQuery().eq(Category::getParentId, "0")));
    }

    @ApiOperation("根据一级分类查询所有子分类")
    @GetMapping("/findCategory2List/{id}")
    public AjaxResult findCategory2ListByParentId(@PathVariable String id) {
        return AjaxResult.success(this.categoryService.list(Wrappers.<Category>lambdaQuery()
                .eq(Category::getParentId, id)));
    }

    @ApiOperation(value = "根据名称查询分类", notes = "返回的值是该分类下所有的子分类")
    @GetMapping("/findCategoryChildListByName/{name}")
    public AjaxResult findCategoryChildListByName(@ApiParam("参数:分类名称") @PathVariable("name") String categoryName) {
        return success(this.categoryService.findCategoryChildListByName(categoryName));
    }

    @ApiOperation("根据id查询分类")
    @GetMapping("/getCategoryInfo/{id}")
    public AjaxResult getCategoryInfoById(@ApiParam("分类id") @PathVariable String id) {
        return success(this.categoryService.getById(id));
    }

    // 不确定有没有用
    @ApiOperation("删除分类")
    @DeleteMapping("/removeCategory/{id}")
    public AjaxResult removeCategory(@ApiParam("删除id") @PathVariable String id) {
        return toAjax(this.categoryService.removeCategoryById(id));
    }
    // 删除分类调用, 非强制删除
    @ApiOperation("删除分裂--非强制删除")
    @DeleteMapping("/removeCategory")
    public AjaxResult removeCategoryNQuit(@ApiParam("删除id集合") @RequestBody List<String> ids) {
        return toAjax(this.categoryService.removeCategoryNQuit(ids));
    }
    // 删除分类调用, 强制删除
    @ApiOperation("删除分类--强制删除")
    @DeleteMapping("/removeCategoryQuit")
    public AjaxResult removeCategoryQuit(@ApiParam("删除id集合") @RequestBody List<String> ids) {
        return toAjax(this.categoryService.removeCategoryQuit(ids));
    }

    @ApiOperation("修改分类")
    @PutMapping("/updateCategory")
    public AjaxResult updateCategory(@ApiParam("分类表单") @RequestBody Category category) {
        return toAjax(this.categoryService.updateCategoryById(category));
    }

    @ApiOperation("新增分类")
    @PostMapping("/saveCategory")
    public AjaxResult saveCategory(@ApiParam("分类表单") @RequestBody Category category) {
        return toAjax(this.categoryService.saveCategory(category));
    }
}
